var WFMessage = new Class({
    Implements: [Options, Events],
    _className: 'wf_message',
    arrayMessage: [],
    currentView: 0, //0 collapse, 1 see all
    initialize: function(dModule){
        this.dModule = dModule;
        if($(this.dModule) === null){
            return;
        }
        if(!$(this.dModule).hasClass(this._className)){
            $(this.dModule).addClass(this._className);
        }
        this.arrayMessage[0] = [];
        this.arrayMessage[1] = [];
        this.arrayMessage[2] = [];
        this.addEvent('message',this.messageEvent.bind(this));
    },
    setDefaultOption:function(){
        this.options = {
            type: '0', //0: error, 1: warn, 2: info
            description: '', //text
            button: '', //string
            callback: function(){}, //callback function
            close: false, //true or false
            name: '',
            reserveRoot: true, //reserve root wf_message node when close message
            containers: []
        };
    },    
    messageEvent: function(options){
        this.setDefaultOption();
		this.setOptions(options);
        if (this.options.containers.length == 0 || (this.options.containers.length > 0 && this.hasContainer(wfcname))){
            // check duplicate
            var duplicate = false;
            this.arrayMessage[options.type].each(function(item,index){
                if(options.description == item.description){
                    duplicate = true;
                    return;
                }
            });
            if(!duplicate){
                this.arrayMessage[options.type].push(options);
                //render ui
                this.render();
            }
        }
    },
    render: function(){
        this.dModule.set('html','');
        this.ul = new Element('ul',{'class':'message'}).inject(this.dModule);
        var number = 0;
        for(var i=0,j=this.arrayMessage.length;i<j;i++){
            for(var k=0,l=this.arrayMessage[i].length;k<l;k++){
                var data = this.arrayMessage[i][k];
                if(data.type == '0'){
                    var type = 'error';
                }
                else if(data.type == '1'){
                    var type = 'warn';
                }
                else if(data.type == '2'){
                    var type = 'info';
                }
                var item = new Element('li',{
                    'html':'<div class="content" name="'+this.options.name+'"><span class="description">'+data.description+'</span></div>'
                }).inject(this.ul);
                item.setStyle('position','relative');
                if(number == 0){
                    item.addClass('first');
                }
                item.addClass(type);
                var me = this;
                if($chk(data.button)){
                    var dButton = new Element('input',{
                        'class': 'button',
                        'type': 'button',
                        'value': data.button,
                        'adata': i+'_'+k
                    }).inject(item.getElement('.content'));
                    dButton.addEvent('click',function(event){
                        event.preventDefault();
                        var adata = this.get('adata');
                        var type = adata.split('_')[0];
                        var order = adata.split('_')[1];
                        me.arrayMessage[type][order].callback();
                    });
                }
                if($chk(data.close)){
                    var dClose = new Element('a',{
                        'class': 'close',
                        'href': '#',
                        'adata': i+'_'+k
                    }).inject(item);
                    dClose.addEvent('click',function(event){
                        event.preventDefault();
                        var adata = this.get('adata');
                        var type = adata.split('_')[0];
                        var order = adata.split('_')[1];
                        me.arrayMessage[type].erase(me.arrayMessage[type][order]);
                        me.number--;
                        me.render();
                    });
                }
                number++;
            }
        }
        this.number = number;
        if(number==0){
            var root = this.ul.getParent('.wf_message');
            this.ul.destroy();
            if (!this.options.reserveRoot){
                root.destroy();
            }
            this.currentView = 0;
        }
        else{
            if(this.currentView==0){
                this.ul.getElements('li').each(function(item,index){
                    if(index>wf_error_number-1){
                        item.set('styles',{'display':'none'});
                    }
                });
                if(number>wf_error_number){
                    this.show = new Element('div',{
                        'class': 'show',
                        'html': '<span class="number">'+this.number+'</span> '+lang.label('total_msg')+' <a href="#" class="see_all">'+lang.label('see_all')+'</a>'
                    }).inject(this.dModule);
                    this.show.getElement('.see_all').addEvent('click',this.showAll.bind(this));
                }
            }
            else if(number>wf_error_number){
                this.show = new Element('div',{
                    'class': 'show',
                    'html': '<a href="#" class="collapse_msg">'+lang.label('collapse_msg')+'</a>'
                }).inject(this.dModule);
                this.show.getElement('.collapse_msg').addEvent('click',this.collapse.bind(this));
            }
        }
    },
    showAll: function(event){
        this.currentView = 1;
        event.preventDefault();
        this.ul.getElements('li').each(function(item,index){
            item.set('styles',{'display':'block'});
        });
        this.show.set('html','<a href="#" class="collapse_msg">'+lang.label('collapse_msg')+'</a>');
        this.show.getElement('.collapse_msg').addEvent('click',this.collapse.bind(this));  
    },
    collapse: function(event){
        this.currentView = 0;
        event.preventDefault();
        this.ul.getElements('li').each(function(item,index){
            if(index>wf_error_number-1){
                item.set('styles',{'display':'none'});
            }
        });
        this.show.set('html','<span class="number">'+this.number+'</span> '+lang.label('total_msg')+' <a href="#" class="see_all">'+lang.label('see_all')+'</a>');
        this.show.getElement('.see_all').addEvent('click',this.showAll.bind(this));
    },
    clear: function(){
        this.arrayMessage[0] = [];
        this.arrayMessage[1] = [];
        this.arrayMessage[2] = [];
        this.render();
    },
    hasContainer:function(container){
        return this.options.containers.contains(container);    
    }
});
